<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="服务编号" prop="serviceSn">
        <el-input v-model="dataForm.serviceSn" placeholder="请输入服务编号"></el-input>
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="dataForm.serviceName" placeholder="请输入服务名称"></el-input>
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input v-model="dataForm.keyword" placeholder="请输入关键字"></el-input>
      </el-form-item>
      <el-form-item label="服务单位" prop="unit">
        <el-input v-model="dataForm.unit" placeholder="请输入服务单位"></el-input>
      </el-form-item>
      <el-form-item label="优惠价格" prop="linePrice">
        <el-input
          v-model="dataForm.linePrice"
          autocomplete="off"
          class="nameInput"
          oninput="value=value.replace(/[^0-9.]/g,'')"   
          placeholder="请输入优惠价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="零售价格" prop="servicePrice">
        <el-input
          v-model="dataForm.servicePrice"
          autocomplete="off"
          class="nameInput"
          oninput="value=value.replace(/[^0-9.]/g,'')"
          placeholder="请输入零售价格"
        ></el-input>
      </el-form-item>
      <el-form-item label="服务简介" prop="brief">
        <el-input  type="textarea" :rows="3" v-model="dataForm.brief" placeholder="请输入服务简介"></el-input>
      </el-form-item>
      <el-form-item label="服务图片" prop="picUrl">
        <single-image-upload prcScale="800x600" module="picUrl" v-model="dataForm.picUrl"></single-image-upload>
      </el-form-item>
      <el-form-item label="宣传图片" prop="gallery">
        <multi-image-upload 
          prcScale="800x600"
          uploadClassName="upload-file1"
          module="gallery"
          :maxCount="3"
          v-model="selectGalleryPics">
        </multi-image-upload>
      </el-form-item>
      <el-form-item label="详情介绍" prop="detail">
        <Tinymce  v-model="dataForm.detail" v-if="visible" />
      </el-form-item>
      <el-form-item label="是否上架" prop="isShelf">
        <el-radio-group v-model="dataForm.isShelf">
          <el-radio :label="1">上架</el-radio>
          <el-radio :label="0">下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="显示顺序" prop="sort">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getServiceInfo, createService, updateService } from '@/api/service/service';
import SingleImageUpload from '@/components/UploadImage/SingleImageUpload';
import MultiImageUpload from '@/components/UploadImage/MultiImageUpload';
import Tinymce from '@/components/Tinymce'

const defaultDataForm = {
  id: '',
  serviceSn: '',
  serviceName: '',
  keyword: '',
  gallery: '',
  brief: '',
  picUrl: '',
  unit: '',
  linePrice: '',
  servicePrice: '',
  detail: '',
  isShelf: '',
  sort: '',
}

export default {
  components: {
    SingleImageUpload,
    MultiImageUpload,
    Tinymce,
  },
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        serviceSn: [
          { required: true, message: '服务编号不能为空', trigger: 'blur' }
        ],
        serviceName: [
          { required: true, message: '服务名称不能为空', trigger: 'blur' }
        ],
        keyword: [
          { required: true, message: '关键字不能为空', trigger: 'blur' }
        ],
        gallery: [
          { required: true, message: '宣传图片不能为空', trigger: 'blur' }
        ],
        brief: [
          { required: true, message: '商品简介不能为空', trigger: 'blur' }
        ],
        picUrl: [
          { required: true, message: '服务图片不能为空', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '服务单位不能为空', trigger: 'blur' }
        ],
        linePrice: [
          { required: true, message: '优惠价格不能为空', trigger: 'blur' }
        ],
        servicePrice: [
          { required: true, message: '零售价格不能为空', trigger: 'blur' }
        ],
        detail: [
          { required: true, message: '详情介绍不能为空', trigger: 'blur' }
        ],
        isShelf: [
          { required: true, message: '是否上架不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  computed: {
      //服务图片
      selectGalleryPics: {
        get: function() {
          let pics = [];
          if (this.dataForm.gallery === undefined 
            || this.dataForm.gallery == null 
            || this.dataForm.gallery === '') {
            return pics;
          }
          let gallerys = this.dataForm.gallery.split(',');
          for (let i = 0; i < gallerys.length; i++) {
            pics.push(gallerys[i]);
          }
          return pics;
        },
        set: function(newValue) {
          if (newValue == null || newValue.length === 0) {
            this.dataForm.gallery = null;
          } else {
            let valueArrays = [];
            for (let i = 0; i < newValue.length; i++) {
              valueArrays.push(newValue[i]);
            }
            this.dataForm.gallery = valueArrays.join(",");
          }
        },
      }
    },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getServiceInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.serviceSn = res.data.serviceSn;
              this.dataForm.serviceName = res.data.serviceName;
              this.dataForm.keyword = res.data.keyword;
              this.dataForm.gallery = res.data.gallery;
              this.dataForm.brief = res.data.brief;
              this.dataForm.picUrl = res.data.picUrl;
              this.dataForm.unit = res.data.unit;
              this.dataForm.linePrice = res.data.linePrice;
              this.dataForm.servicePrice = res.data.servicePrice;
              this.dataForm.detail = res.data.detail;
              this.dataForm.isShelf = res.data.isShelf;
              this.dataForm.sort = res.data.sort;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            serviceSn: this.dataForm.serviceSn,
            serviceName: this.dataForm.serviceName,
            keyword: this.dataForm.keyword,
            gallery: this.dataForm.gallery,
            brief: this.dataForm.brief,
            picUrl: this.dataForm.picUrl,
            unit: this.dataForm.unit,
            linePrice: this.dataForm.linePrice,
            servicePrice: this.dataForm.servicePrice,
            detail: this.dataForm.detail,
            isShelf: this.dataForm.isShelf,
            sort: this.dataForm.sort,
          }
          if (!this.dataForm.id) {
            // 创建
            createService(params).then(response => {

              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false;
              this.$emit('refreshDataList')

            })
          } else {
            // 更新
            updateService(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false;
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
